package Lrcshow;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Vector; 
import javax.swing.JFrame;
import javax.swing.JLabel;

import Music_View.music_view;
import util.TextLyrics;  
public class LrcData extends Thread 
{	
	static BufferedReader bufferReader = null;	
	public String title = "";	
	public String artist = "";	
	public String album = "";	
	public String lrcMaker = "";	
	public static long time2;	
	public static long time1 = 0;	
	public static JFrame jf = null;	
	public static boolean run = true;	
	public static boolean endLrc = false;	
	public static LrcData lrcData = new LrcData(); 	
	public FileInputStream file = null;
	public static long timeTemp = 0;	
	public String lrcPathpppp = null;	
	public static Vector<LrcDao> statements = new Vector<LrcDao>();	
	static LrcDao sm;
	public static ArrayList<JLabel> s = new ArrayList();;
	/*	
	 * 实例化一个歌词数据，歌词数据信息由指定的文件提供 filename ：指定的歌词文件	 
	 */	
	
	public static void setPath(String filename){	
		try {			
			// 先转化为文件流	
			FileInputStream file = new FileInputStream(filename);	
			// 再转化为bufferReader	
			bufferReader = new BufferedReader(new InputStreamReader(file, "GB2312"));	
			// 将文件数据读入内存	
			readData();	
			s = TextLyrics.GetTextLyrics();
		} 
		catch (FileNotFoundException e) {
			e.printStackTrace();	
		}catch (UnsupportedEncodingException e){	
			e.printStackTrace();	
		}	
	}	
	private LrcData() {	
		
	}	
	public static LrcData getLrcData() {		
		return lrcData;
	} 	
	
	public LrcData(String filename) {	
		bufferReader = null;
		try{			// 先转化为文件流	
			file = new FileInputStream(filename);	
			// 再转化为bufferReader	
			bufferReader = new BufferedReader(new InputStreamReader(file, "GB2312"));	
			// 将文件数据读入内存						
			readData();					
			} catch (FileNotFoundException e) {			
				e.printStackTrace();		
			} catch (UnsupportedEncodingException e) {			
					e.printStackTrace();		
			}	
		}
	
	public static void readData() {		
		statements.clear();		
		String strLine = "";		
		// 循环读入所有行		
		try {			
			while (null != (strLine = bufferReader.readLine())){				
				// 判断该行是否为空行				
				if ("".equals(strLine.trim())){					
					continue;				
				}				
				int timeNum = 0; 
				// 本行含时间个数				
				String str[] = strLine.split("\\]"); 
				// 以]分隔				
				for (int i = 0; i < str.length; ++i){					
					String str2[] = str[i].split("\\["); 
					// 以[分隔					
					str[i] = str2[str2.length - 1];					
					if (isTime(str[i])){						
						++timeNum;					
					}									
					}							
				for (int i = 0; i < timeNum; ++i) 
					// 处理歌词复用的情况				
					{		
					sm = new LrcDao();	
					sm.setTime(str[i]);					
					if (timeNum < str.length) 
						// 如果有歌词					
					{						
						sm.setLyric(str[str.length - 1]);					
					}					
					statements.add(sm);				
					}
//					for(int y=0;y<3;y++) {
//						  sm = new LrcDao();
//						  sm.setTime[]
//					}
				  	
				    
				}
			} catch (IOException e) {		
				e.printStackTrace();		
		}	
	}	
	
	private static boolean isTime(String string) {		
		String str[] = string.split(":|\\.");
		//有关split的具体用法请上百度查		
		if (3 != str.length) {			
			return false;		
		}		
		try {		
			//确保这三段字符串都是时间，			
			for (int i = 0; i < str.length; ++i){				
				Integer.parseInt(str[i]);	
				}	
			} catch (NumberFormatException e){			
				return false;		
			}	
		   return true;
		}	
	
	public static void printLrcDate(int i)
	{		
		int y=7;
		music_view.getUpsongshow().setText(statements.elementAt(i).getLyric());
		if(i<3) {
			for(int f=0;f<3-i;f++) {
			  s.get(6-f).setText("");
			}
			y=7-3+i;
		}
		  for(int t=0;t<y;t++) {
			  if(statements.size()>i-t+3) {
				  System.out.println(t+statements.elementAt(i-t+3).getLyric());
				  s.get(t).setText(statements.elementAt(i-t+3).getLyric());
			  } else {
				  System.out.println(t);
				  s.get(t).setText("");
			  }
		  }
		
	}
  }
	



